


/******************************************************************************
*******************************************************************************
*******************************************************************************
Preparation
*******************************************************************************
*******************************************************************************
*******************************************************************************/

/******************************************************************************
*******************************************************************************
Paths and global parameters
*******************************************************************************
*******************************************************************************/

/*****************
Primary [YOU MUST SET THIS TO POINT TO ROOT FOLDER]
******************/
cd "E:\Work\Dropbox\work\replication\prodestcon"

/*****************
Empirical
******************/
//Path of restricted data
global data "E:/factors/data/"

//Path to store restricted intermediate datasets, and with sepfact.dta from Shenoy (2017) replication data
global path "E:/factors/test3/"




/*****************
Simulation: Reps & cores
******************/
global ncores = 24

//Number of iterations for each piece of the parameter space [parallel operations]
global B = 30

//Number of iterations for each piece of the parameter space [serial]
global Bser = 300



/*****************
Simulation: Constructing criterion
******************/
//Cutoff p-value for specification test
global testcutoff 	.01




/*****************
Make directories and save globals
******************/
cap mkdir parlog
cap mkdir dta/simulations
cap mkdir dta/emp_results


/*
Write globals
*/
cap mkdir constants
clear
	set obs 3
	gen 	command = "global ncores ${ncores}" in 1
	replace command = "global B ${B}" in 2
	replace command = "global Bser ${Bser}" in 3 
	export delimited using "constants/simglobals.do", novarn replace
clear


/******************************************************************************
*******************************************************************************
Flags for which parts to (re)run
*******************************************************************************
*******************************************************************************/
//Calibration
local RUNCAL  1

//Simulations
local RUNSIM1 1
local RUNSIM2 1
local RUNSIM3 1
local RUNSIM4 1
local RUNSIM5 1
local RUNSIM6 1
local RUNSIM7 1



/******************************************************************************
*******************************************************************************
Programs
*******************************************************************************
*******************************************************************************/

/******************************************************************************
Combine
*******************************************************************************/
cap program drop combinedta
program combinedta
	args tocombine

	clear
	gen src = ""
	folders "dta/simulations/`tocombine'"
	foreach corerun in `r(folders)' {
		folders "dta/simulations/`tocombine'/`corerun'"
		foreach spec in `r(folders)' {
			fs "dta/simulations/`tocombine'/`corerun'/`spec'/*.dta"
			foreach file in `r(files)' {
				append using "dta/simulations/`tocombine'/`corerun'/`spec'/`file'"
				replace src = "dta/simulations/`tocombine'/`corerun'/`spec'/`file'" if mi(src)
			}
		}
	}

	save "dta/simulations/`tocombine'.dta", replace
end


/******************************************************************************
Compute Fraction Constrained
*******************************************************************************/

cap program drop constraint_fractions
program constraint_fractions
	clear
		

	folders "dta/exdata/1_1"
	foreach confol in `r(folders)' {
		noisily: di _n _n "On folder `confol'" _c
		quietly {
			fs "dta/exdata/1_1/`confol'/*.dta"
			
			foreach file in `r(files)' {
		
				use "dta/exdata/1_1/`confol'/`file'", clear
				
				gen mfrac = M/Mopt
				
				//Unconditional
				gen uncond_max = constrained==0
				forvalues q = 20(20)100 {
					local lb = (`q'-20)/100
					local ub = `q'/100
					
					gen uncond_q`q' =  (mfrac >= `lb') & (mfrac < `ub')
				}
				
				
				//Conditional on being constrained
				forvalues q = 20(20)100 {
					local lb = (`q'-20)/100
					local ub = `q'/100
					
					gen cond_q`q' =  (mfrac >= `lb') & (mfrac < `ub') if constrained == 1
				}
				
				
				collapse uncond* cond*
				gen conset = "`confol'"
				
				cap confirm file `base'
				if _rc == 0 {
					append using `base'
					save `base', replace
				}
				else {
					tempfile base
					save `base'
				}
				
				noisily: di "." _c
			}
			
		}
	}

	save "dta/constraint_fractions.dta", replace

end






/******************************************************************************
*******************************************************************************
*******************************************************************************
Calibration
*******************************************************************************
*******************************************************************************
*******************************************************************************/
if "`RUNCAL'" == "1" {
	do "do/prodestcon_calibrate2.do"
	cap rm "miss_data_jan_28_2011.dta"
}




/******************************************************************************
*******************************************************************************
*******************************************************************************
Run Simulations
*******************************************************************************
*******************************************************************************
*******************************************************************************/

/******************************************************************************
*******************************************************************************
Main Text
*******************************************************************************
*******************************************************************************/

/******************************************************************************
1. Primary simulation
*******************************************************************************/
if "`RUNSIM1'" == "1" {
	di "Running 1. Primary simulation"
	shell sh sh/feasiblerr ${ncores}
	combinedta sweeps_feasible_widrev
}


/******************************************************************************
*******************************************************************************
Appendix
*******************************************************************************
*******************************************************************************/

/******************************************************************************
2. Nonlinear simulation, calibrated to 311
*******************************************************************************/
if "`RUNSIM2'" == "1" {
	di "Running 2. Nonlinear simulation, calibrated to 311"
	shell sh sh/feasiblerr_nl ${ncores}
	combinedta sweeps_feasible_widrev_nl
}

/******************************************************************************
3. Extreme parameter simulation
*******************************************************************************/
if "`RUNSIM3'" == "1" {
	di "3. Extreme parameter simulation"
	shell sh sh/systematic ${ncores}
	combinedta sweeps_systematicparam_widrev
}



/******************************************************************************
4. Test stats
*******************************************************************************/
if "`RUNSIM4'" == "1" {
	di "4. Test stats"
	do "do/simulate_teststatgen.do"
}


/******************************************************************************
5. Extreme nonlinearity
*******************************************************************************/
if "`RUNSIM5'" == "1" {
	di "5. Extreme nonlinearity"
	shell sh sh/nlrev ${ncores}
	combinedta sweeps_nlsim_rev
}


/******************************************************************************
6. Extensive vs. intensive margin constraints
*******************************************************************************/
if "`RUNSIM6'" == "1" {
	do "do/simulate_example_datasets.do"
	constraint_fractions
	shell sh sh/bivar ${ncores}
	combinedta sweeps_feasible_bivar2
}

/******************************************************************************
7. Compare autoregressive to ACF
*******************************************************************************/
if "`RUNSIM7'" == "1" {
	di "7. Compare autoregressive to ACF"
	shell sh sh/va ${ncores}
	combinedta sweeps_env_va
}


/******************************************************************************
*******************************************************************************
*******************************************************************************
Create exhibits
*******************************************************************************
*******************************************************************************
*******************************************************************************/
do "do/analysis_and_exhibits.do"
